<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/4/23
 * Time: 17:55
 */
namespace app\api\controller;

use app\api\model\verticalDb;
use think\Controller;
use think\Request;
use think\Db;

/**
 * 验证所有api接口字段
 */
class Verifyfield{

    /**
     * 验证是否uid和sKey
     */
    function verifyUser($uid,$sKey){
        $u_where = [
            'uid'=>$uid,
            'S_key'=>$sKey,
        ];
        $u_data = Db::table('role_info')->where($u_where)->find();
        if (empty($u_data)) {
            return false;
        }else{
            return true;
        }
    }

    /**
     * 验证
     */
    public function field1($param){
        if(!$param['uid']){
           return 'uid参数不能为空';
        }
        if(!$param['sKey']){
            return 'sKey参数不能为空';
        }
        if(!$param['idCardNo']){
            return 'idCardNo参数不能为空';
        }
        if(!$param['realName']){
            return 'realName参数不能为空';
        }
        return '';
    }
    public function field2($param){
        if(!$param['account']){
            return 'account参数不能为空';
        }
        return '';
    }
    public function field3($param){
        if(!$param['uid']){
            return 'uid参数不能为空';
        }
        if(!$param['sKey']){
            return 'sKey参数不能为空';
        }
        if(!$param['scoreId']){
            return 'scoreId参数不能为空';
        }
        return '';
    }
    /**
     * 验证身份证和姓名
     */
    public function vfSfz($param){
        $curl = curl_init();
        $appKey = 'f3c5a8e6072b4b3f95873ca4ac4bc009';
        $url = "http://apis.haoservice.com/idcard/VerifyIdcard?cardNo=".$param['idCardNo']."&realName=".$param['realName']."&key=".$appKey;
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        $response = json_decode($response,true);
        if ($response['error_code'] != 0) {
            return $response['reason'].'.';
        } else {
            if($response['result']['isok'] == true){
                return '';
            }else{
                return '验证失败';
            }
        }
    }

    //随机生成N位数字
    function nonceStr($n) {
        static $seed = array(0,1,2,3,4,5,6,7,8,9);
        $str = '';
        for($i=0;$i<$n;$i++) {
            $rand = rand(0,count($seed)-1);
            $temp = $seed[$rand];
            $str .= $temp;
            unset($seed[$rand]);
            $seed = array_values($seed);
        }
        return $str;
    }

}